<?php

namespace app\applet\controller;

use app\applet\BaseController;

class CommonController extends BaseController
{
    /**
     * @Description 获取所有省份
     *
     * @return void
     */
    public function provinceAction()
    {
        try{
            $res = $this->app->commonService->getProvince();
            $data = [];
            foreach($res as $key=>$vo){
                $arr = [];
                $arr['provinceId'] = $vo->provinceId;
                $arr['provinceCode'] = $vo->provinceCode;
                $arr['provinceName'] = $vo->provinceName;
                $data[] = $arr;
            }
            return json(['code' => 200, 'msg' => '查询成功', 'data'=> $data]);
        }catch(\Exception $e){
            return json(['code' => $e->getCode(), 'msg' => $e->getMessage(), 'data'=> []]);
        }
    }

    /**
     * @Description 根据省份code获取城市
     * @param  [type] $code
     *
     * @return void
     */
    public function cityAction()
    {
        try{
            $provinceId = isset($this->get['provinceId'])?$this->get['provinceId']:'';

            $cities = $this->app->commonService->getCities($provinceId);
            $data = [];
            foreach($cities as $city){
                $arr = [];
                $arr['cityId'] = $city->cityId;
                $arr['cityName'] = $city->cityName;
                $arr['cityCode'] = $city->cityCode;
                $data[] = $arr;
            }
            return json(['code' => 200, 'msg' => '查询成功', 'data'=> $data]);
        }catch(\Exception $e){
            return json(['code' => $e->getCode(), 'msg' => $e->getMessage(), 'data'=> []]);
        }
    }
    /**
     * @Description 根据城市code获取区县
     * @param  [type] $code
     *
     * @return void
     */
    public function areaAction()
    {
        try{
            $cityId = isset($this->get['cityId'])?$this->get['cityId']:'';

            $areas = $this->app->commonService->getAreas($cityId);
            $data = [];

            foreach($areas as $area){
                $arr = [];
                $arr['areaId'] = $area->areaId;
                $arr['areaName'] = $area->areaName;
                $arr['areaCode'] = $area->areaCode;
                $data[] = $arr;
            }

            return json(['code' => 200, 'msg' => '查询成功', 'data'=> $data]);
        }catch(\Exception $e){
            return json(['code' => $e->getCode(), 'msg' => $e->getMessage(), 'data'=> []]);
        }
    }
    
}
